﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
	public partial class FormMain : Form
	{
		private BackupFiles bf;
		private int currentPos = -1;

		public FormMain()
		{
			InitializeComponent();

			splitContainer1.Panel2Collapsed = true;

			bf = new BackupFiles(this, 300);
			bf.Progress += new EventHandler<BackupEventArgs>(bf_Progress);
			bf.BackupComplete += new EventHandler<CompleteEventArgs>(bf_BackupComplete);
			bf.ErrorOccurred += new EventHandler<ErrorEventArgs>(bf_ErrorOccurred);
			Utils.LoadJobs(dataSetBackup.Progress);
		}

		void bf_ErrorOccurred(object sender, ErrorEventArgs e)
		{
			splitContainer1.Panel2Collapsed = false;
			DataSetBackup.ErrorsRow row = dataSetBackup.Errors.NewErrorsRow();
			row.jobname = e.JobName;
			row.errortype = e.ErrorType.ToString();
			row.description = e.Ex.Message;
			row.time = DateTime.Now;
			row.errorobject = e.CurrentPath;
			dataSetBackup.Errors.AddErrorsRow(row);
		}

		void bf_BackupComplete(object sender, CompleteEventArgs e)
		{
			if (currentPos >= 0)
			{
				DataSetBackup.ProgressRow row = (DataSetBackup.ProgressRow)dataSetBackup.Progress.Rows[currentPos];
				row.status = "Done";
				row.filesprocessed = e.FilesProcessed;
				row.bytesprocessed = e.BytesProcessed;
				row.elapsedtime = e.ElapsedTime;
				if (e.ElapsedTime.TotalSeconds > 0)
				{
					row.filespersecond = e.FilesProcessed / e.ElapsedTime.TotalSeconds;
					row.bytespersecond = e.BytesProcessed / e.ElapsedTime.TotalSeconds;
					row.bytespersecondstring = Utils.FormatBytes(Convert.ToInt64(row.bytespersecond));
					row.bytesprocessedstring = Utils.FormatBytes(row.bytesprocessed);
				}
				currentPos++;
				StartBackupProcedure();
			}
		}

		void bf_Progress(object sender, BackupEventArgs e)
		{
			if (currentPos >= 0)
			{
				DataSetBackup.ProgressRow row = (DataSetBackup.ProgressRow)dataSetBackup.Progress.Rows[currentPos];
				row.filesprocessed = e.FilesProcessed;
				row.bytesprocessed = e.BytesProcessed;
				row.elapsedtime = e.ElapsedTime;
				row.file = e.Message;
				if (e.ElapsedTime.TotalSeconds > 0)
				{
					row.filespersecond = e.FilesProcessed / e.ElapsedTime.TotalSeconds;
					row.bytespersecond = e.BytesProcessed / e.ElapsedTime.TotalSeconds;
					row.bytespersecondstring = Utils.FormatBytes(Convert.ToInt64(row.bytespersecond));
					row.bytesprocessedstring = Utils.FormatBytes(row.bytesprocessed);
				}
			}
		}

		private void startCancelButton1_Click(object sender, EventArgs e)
		{
			if (startCancelButton1.State == ButtonState.Start)
			{
				startCancelButton1.State = ButtonState.Cancel;
                startCancelButton1.ImageIndex = 1;

				dataSetBackup.Errors.Clear();
				splitContainer1.Panel2Collapsed = true;

				currentPos = 0;
				ResetStatus();
				StartBackupProcedure();
			}
			else
			{
				bf.Cancel();
				currentPos = -1;
				startCancelButton1.State = ButtonState.Start;
                startCancelButton1.ImageIndex = 0;
			}
		}

		private void StartBackupProcedure()
		{
			if (currentPos >= 0 && currentPos < dataSetBackup.Progress.Rows.Count)
			{
				DataSetBackup.ProgressRow row = (DataSetBackup.ProgressRow)dataSetBackup.Progress.Rows[currentPos];
				row.status = "In progress";
				bf.Backup(row.origin, row.destination, row.jobname, row.copyonlynewfiles);
			}
			else
			{
				startCancelButton1.State = ButtonState.Start;
                startCancelButton1.ImageIndex = 0;
				currentPos = -1;
			}
		}

		private void ResetStatus()
		{
			foreach (DataSetBackup.ProgressRow row in dataSetBackup.Progress.Rows)
			{
				row.status = "";
			}
		}

		private void buttonSave_Click(object sender, EventArgs e)
		{
			Utils.SaveJobs(dataSetBackup.Progress);
		}
	}
}